<template>
  <!-- 轮播图弹窗 -->
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false" width="600px">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="110px">
      <el-form-item prop="courseName" label="课程名称">
        <el-input v-model.trim="dataForm.courseName" placeholder="课程名称"></el-input>
      </el-form-item>

      <el-form-item prop="count" label="课程节数">
        <el-input-number v-model="dataForm.count" :min="1" controls-position="right" />
      </el-form-item>
      <el-form-item prop="hours" label="总课时数">
        <el-input-number v-model="dataForm.hours" :min="0" :precision="2" controls-position="right" />
      </el-form-item>
      <el-form-item prop="courseDesc" label="课程描述">
        <el-input v-model.trim="dataForm.courseDesc" type="textarea" :autosize="{ minRows: 5, maxRows: 20 }" placeholder="课程描述"></el-input>
      </el-form-item>
      <el-form-item prop="courseSimpleDesc" label="课程简介">
        <el-input v-model.trim="dataForm.courseSimpleDesc" type="textarea" :autosize="{ minRows: 5, maxRows: 20 }" placeholder="课程简介"></el-input>
      </el-form-item>
      <el-form-item prop="bgUrl" label="课程封面地址">
        <div class="updateImg-box">
          <updateImg ref="uploadRef" :urlList="dataForm.urlList" @updatImgList="updatImgList"></updateImg>
        </div>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, nextTick } from "vue";
import baseService from "@/service/baseService";
import { IObject } from "@/types/interface";
import { ElMessage } from "element-plus";
import updateImg from "@/components/updateImg.vue";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const uploadRef = ref();

const dataForm = reactive<IObject>({
  id: "",
  courseName: "",
  count: 0,
  hours: 0,
  courseDesc: "",
  courseSimpleDesc: "",
  bgUrl: "",
  urlList: []
});

const rules = ref({
  courseName: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  count: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  hours: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  courseDesc: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  courseSimpleDesc: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  bgUrl: [{ required: true, message: "必填项不能为空", trigger: "blur" }]
});

const init = async (id?: number) => {
  console.log(333);

  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataForm.urlList = [];
    dataFormRef.value.resetFields();
  }

  if (id) {
    await getInfo(id);
  }

  await nextTick(() => {
    uploadRef.value.init();
  });
};

// 获取信息
const getInfo = async (id: number) => {
  await baseService.get(`/lingyao/courseinfo/${id}`).then((res) => {
    res.data.urlList = res.data.bgUrl
      ? [
          {
            name: "轮播图",
            url: res.data.bgUrl
          }
        ]
      : [];
    Object.assign(dataForm, res.data);
  });
};

// 上传图片成功
const updatImgList = (list: any[]) => {
  dataForm.bgUrl = list.length > 0 ? list[0].url : "";
  dataForm.urlList = list;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/lingyao/courseinfo", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
